home *** CD-ROM | disk | FTP | other *** search
- #include <abc.h>
-
- #include <MacTypes.h>
- #include <QuickDraw.h>
- #include <EventMgr.h>
- #include <WindowMgr.h>
- #include <DialogMgr.h>
- #include <MemoryMgr.h>
- #include <ResourceMgr.h>
- #include <StdFilePkg.h>
- #include <FileMgr.h>
-
- Ptr getMap();
-
- #define PixelWidth 4
- #define PixelHeight 4
-
- WindowRecord wRecord;
- WindowPtr theWindow;
- EventRecord theEvent;
-
- short nRows,
- nCols;
- char theString[256];
-
- char *hexDigits = "0123456789ABCDEF",
- *string1 = "\Ptype BMAP=GNRL\r,128\r",
- *string2 = "\P.I ;; nRows…\r",
- *string3 = "\P.I ;; nCols…\r",
- *string4 = "\P\r",
- *string5 = "\P.H ;; the BitMap data itself…\r";
-
- main()
- {
- Ptr theData;
-
- initialize();
- theData = getMap();
- if ( theData NEQ NULL ) {
- editMap( theData );
- writeMap( theData );
- }
- } /* main */
-
- initialize()
- {
- InitGraf ( &thePort );
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs ( NULL );
- InitCursor();
- } /* initialize */
-
- /*
- This routine allocates memory for the requested bitmap and intializes
- it to zero.
- */
- Ptr
- getMap()
- {
- register Ptr theData;
- register short i;
-
- Handle tempHandle;
- Size theMapSize;
-
- nRows = nCols = 32;
-
- tempHandle = GetResource ( 'ICON', noteIcon );
- if ( tempHandle EQ NULL )
- return NULL;
-
- DetachResource ( tempHandle );
- HLock ( tempHandle );
- return *tempHandle;
-
- } /* getMap */
-
- editMap ( theData )
- Ptr theData;
- {
- WindowPtr wWindow;
- BitMap theBitMap;
- Rect wRect;
- Boolean done;
- short thePart;
- char theChar;
-
- /* Create a window in which to display the BitMap in FatBits */
- SetRect ( &wRect, 50, 50, 50+nCols*PixelWidth, 50+nRows*PixelHeight );
- theWindow = NewWindow ( &wRecord, &wRect, "\PBitMap Editor", TRUE,
- 4, -1L, TRUE, 0L );
-
- /* initialize the BitMap record, using the pointer we were given */
- theBitMap.baseAddr = theData;
- theBitMap.rowBytes = ( ((nCols - 1) / 16) + 1) * 2;
- SetRect ( &theBitMap.bounds, 0, 0, nCols, nRows );
-
- /* process mouse events until user dismisses window */
- done = FALSE;
- do {
- if ( GetNextEvent ( everyEvent, &theEvent ) ) {
- switch ( theEvent.what ) {
- case mouseDown:
- thePart = FindWindow ( theEvent.where, &wWindow );
- if ( wWindow EQ theWindow ) {
- switch ( thePart ) {
- case inContent:
- editBits ( theWindow, &theBitMap );
- break;
- case inGoAway:
- if ( TrackGoAway ( wWindow, theEvent.where ) ) {
- HideWindow ( wWindow );
- done = TRUE;
- }
- } /* switch ( thePart ) */
- } /* if wWindow EQ theWindow */
- break;
-
- case keyDown:
- theChar = theEvent.message; /* note type coersion */
- switch ( theChar ) {
- case BS:
- EraseRect ( &theWindow->portRect );
- InvalRect ( &theWindow->portRect ); /* force update event */
- break;
- case RETURN:
- case ENTER:
- done = TRUE;
- break;
- default:
- SysBeep(2);
- break;
- }
- break;
-
- case activateEvt:
- SetPort ( theEvent.message );
- break;
-
- case updateEvt:
- doUpdate ( theWindow, &theBitMap );
- break;
- } /* switch ( theEvent.what ) */
- } /* if GetNextEvent() */
- } while ( NOT done );
-
-
- } /* editMap */
-
- doUpdate ( badWindow, theBitMap )
- WindowPtr badWindow;
- BitMap *theBitMap;
- {
- register char *theData;
- register short j, k, rowBytes;
- register char tempChar;
-
- short i;
- GrafPtr savePort;
- Rect pixelRect;
-
- GetPort ( &savePort );
- SetPort ( badWindow );
-
- SetRect ( &pixelRect, 1, 1, PixelWidth, PixelHeight );
-
- theData = theBitMap->baseAddr;
- rowBytes = theBitMap->rowBytes;
-
- BeginUpdate ( badWindow );
-
- /* Draw grid dots… */
- for ( k = 0; k <= nRows; k++ ) {
- MoveTo ( 0, k*PixelHeight );
- for ( i = nCols; i >= 0; i-- ) {
- Move ( PixelWidth, 0 );
- Line ( 0, 0 );
- }
- }
-
- /* Fill in contents of current BitMap */
- for ( j = 0; j < nRows; j++ ) {
- for ( i = 0; i < rowBytes; i++ ) {
- tempChar = theData[i+j*rowBytes];
- for ( k = 0; k < 8; k++ ) {
- if ( tempChar bAND 0x80 )
- PaintRect ( &pixelRect );
- OffsetRect ( &pixelRect, PixelWidth, 0 );
- tempChar = tempChar << 1;
- } /* for (k) */
- } /* for (i) */
- OffsetRect ( &pixelRect, -rowBytes*8*PixelWidth, PixelHeight );
- } /* for (j) */
-
- EndUpdate ( badWindow );
-
- } /* doUpdate */
-
- editBits ( bitWindow, theBitMap )
- WindowPtr bitWindow;
- BitMap *theBitMap;
- {
- register char *theData;
-
- GrafPtr myPort;
- short theError;
- Point mouseLoc;
- Rect pixelRect;
- short vHit, hHit,
- dh, dv,
- BorW;
- Boolean done;
-
- /* rectangle for drawing "Fat" pixel… */
- SetRect ( &pixelRect, 1, 1, PixelWidth, PixelHeight );
-
- /* make a register copy of theBitMap->baseAddr */
- theData = theBitMap->baseAddr;
-
- /* get memory for the GrafPort */
- myPort = (GrafPtr) NewPtr ( SIZEOF(GrafPort) );
- if ( MemError() ) { SysBeep(2); return; }
-
- /* Initialize "myPort" and install "theBitMap" */
- OpenPort ( myPort );
- SetPort ( myPort ); /* Is this necessary? */
- SetPortBits ( theBitMap );
-
- /*
- First, the mouse location is recorded and tested. If it is in the active
- area, drawing begins, otherwise, we return.
- */
- SetPort ( bitWindow );
-
- GetMouse ( &mouseLoc );
- dh = ( hHit = ( mouseLoc.h )/PixelWidth ) * PixelWidth;
- dv = ( vHit = ( mouseLoc.v )/PixelHeight ) * PixelHeight;
- if ( vHit < 0 OR vHit >= nRows OR hHit < 0 OR hHit >= nCols ) {
- ClosePort ( myPort );
- DisposPtr ( myPort );
- SysBeep(2);
- return; /* mouseDown was not in active area */
- }
- SetPort ( myPort );
- BorW = GetPixel ( hHit, vHit ) ? patBic : patCopy;
- PenMode ( BorW );
- SetPort ( bitWindow );
- PenMode ( BorW );
-
- /* The drawing begins... */
- done = FALSE;
- do {
- if ( NOT Button() )
- done = TRUE;
- else {
- GetMouse ( &mouseLoc );
- dv = ( vHit = ( mouseLoc.v )/PixelHeight ) * PixelHeight;
- dh = ( hHit = ( mouseLoc.h )/PixelWidth ) * PixelWidth;
- if ( vHit < 0 OR vHit >= nRows OR hHit < 0 OR hHit >= nCols )
- ;
- else /* the mouse is in the drawing area… */{
- SetPort ( myPort );
- /* Does the hit bit need to be modified? */
- if ( BorW EQ (GetPixel(hHit,vHit)?patBic:patCopy) ) {
- /* First, modify the "theData" through "myPort" */
- MoveTo ( hHit, vHit ); Line ( 0, 0 );
- /* Then modify the (visible) window */
- SetPort ( bitWindow );
- OffsetRect ( &pixelRect, dh, dv);
- PaintRect ( &pixelRect );
- OffsetRect ( &pixelRect, -dh, -dv);
- } else
- SetPort ( bitWindow ); /* For the next time through the loop */
- }
- }
- } while ( NOT done );
-
- SetPort ( bitWindow );
- PenMode ( patCopy );
-
- ClosePort ( myPort );
- DisposPtr ( myPort );
-
- } /* editBits */
-
- /*
- This routine writes out the contents of the bitMap in hexadecimal
- in a format which RMaker will take.
- */
- writeMap ( theData )
- register unsigned char *theData;
- {
- register i, j, rowBytes;
-
- SFReply theReply;
- short theErr,
- fPathNum;
- long writeSize;
- unsigned char theChar,
- oneWord[2];
-
- SFPutFile ( 0x00640064,"\PSave Data File As…","\PUntitled",
- NULL,&theReply);
- if ( NOT theReply.good )
- return;
-
- if ( theErr = FSOpen ( theReply.fName, theReply.vRefNum, &fPathNum ) ) {
- if ( theErr NEQ fnfErr )
- return;
- if ( theErr = Create ( theReply.fName, theReply.vRefNum, 'BmEd', 'TEXT' ) )
- return;
- if ( theErr = FSOpen ( theReply.fName, theReply.vRefNum, &fPathNum ) )
- return;
-
- }
- if ( theErr = SetEOF ( fPathNum, 0L ) )
- return;
- if ( theErr = SetFPos ( fPathNum, fsFromStart, 0L ) ) goto err;
-
- /* write type and ID number… */
- writeSize = string1[0];
- if ( theErr = FSWrite ( fPathNum, &writeSize, string1+1 ) ) goto err;
-
- /* write number of rows… */
- writeSize = string2[0];
- if ( theErr = FSWrite ( fPathNum, &writeSize, string2+1 ) ) goto err;
- NumToString ( (long) nRows, theString );
- writeSize = theString[0];
- if ( theErr = FSWrite ( fPathNum, &writeSize, theString+1 ) ) goto err;
- writeSize = string4[0];
- if ( theErr = FSWrite ( fPathNum, &writeSize, string4+1 ) ) goto err;
-
- /* write number of cols… */
- writeSize = string3[0];
- if ( theErr = FSWrite ( fPathNum, &writeSize, string3+1 ) ) goto err;
- NumToString ( (long) nCols, theString );
- writeSize = theString[0];
- if ( theErr = FSWrite ( fPathNum, &writeSize, theString+1 ) ) goto err;
- writeSize = string4[0];
- if ( theErr = FSWrite ( fPathNum, &writeSize, string4+1 ) ) goto err;
-
- rowBytes = ( ( nCols - 1 ) / 16 + 1 ) * 2;
- writeSize = string5[0];
- if ( theErr = FSWrite ( fPathNum, &writeSize, string5+1 ) ) goto err;
- for ( i = 0; i < nRows; i++ ) {
- writeSize = 2;
- for ( j = 0; j < rowBytes; j++ ) {
- theChar = theData[i*rowBytes+j];
- oneWord[1] = hexDigits [ theChar bAND 0x0F ];
- oneWord[0] = hexDigits [ theChar >> 4 ];
- if ( theErr = FSWrite ( fPathNum, &writeSize, oneWord ) ) goto err;
- }
- writeSize = string4[0];
- if ( theErr = FSWrite ( fPathNum, &writeSize, string4+1 ) ) goto err;
- }
- writeSize = string4[0];
- if ( theErr = FSWrite ( fPathNum, &writeSize, string4+1 ) ) goto err;
-
- /*
- I hate goto's but this seemed the slickest way to do what I wanted
- with minimal effort.
- */
- err: /* close the file and return */
- FSClose ( fPathNum );
- return;
-
- } /* writeMap */
-